package org.ax.service.device.imple;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.ax.mapper.device.DeviceFaultMapper;
import org.ax.pojo.device.DeviceFault;
import org.ax.service.device.DeviceFaultService;
import org.ax.service.device.DeviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.swing.*;
import java.util.Arrays;
import java.util.List;

/**
 * 编程人:赵浩鑫
 * 时间:2021-03-03-14:59
 */
@Service
public class IDeviceFaultService extends ServiceImpl<DeviceFaultMapper, DeviceFault> implements DeviceFaultService {
    @Autowired
    DeviceService deviceService;
    @Override
    public Page<DeviceFault> searchDeviceFaults(Page page, String deviceId) {
        QueryWrapper<DeviceFault> queryWrapper=new QueryWrapper<DeviceFault>();
        queryWrapper.eq(!StringUtils.isBlank(deviceId), "device_id",deviceId);
        Page page1 = getBaseMapper().selectPage(page, queryWrapper);
        List<DeviceFault> deviceFaults=(List<DeviceFault>)page.getRecords();
        for (DeviceFault deviceFault : deviceFaults) {
            deviceFault.setDevice(deviceService.toDeviceId(deviceFault.getDeviceId()));
        }
        return page1;
    }

    @Override
    public boolean addDeviceFault(DeviceFault deviceFault) {
        return getBaseMapper().insert(deviceFault)>0;
    }

    @Override
    public boolean updateDeviceFault(DeviceFault deviceFault) {
        return getBaseMapper().updateById(deviceFault)>0;
    }

    @Override
    public boolean deletesDeviceFault(String[] ids) {
        return getBaseMapper().deleteBatchIds(Arrays.asList(ids))>0;
    }

    @Override
    public DeviceFault toDeviceFaultId(String id) {
        return getBaseMapper().selectById(id);
    }

    @Override
    public List<DeviceFault> findDeviceFaults() {
        return getBaseMapper().selectList(null);
    }
}
